package com.winbaoxian.common.freemarker.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author dongxuanliang252
 * @date 2021-04-13 13:46
 */
public enum NameUtils {

    INSTANCE;

    private static final Pattern TO_LINE_PATTERN = Pattern.compile("[A-Z]+");
    private static final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])");

    public String sakeCase(String text) {
        if (StringUtils.isEmpty(text)) {
            return text;
        }
        Matcher matcher = TO_LINE_PATTERN.matcher(text);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            if (matcher.start() > 0) {
                matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase());
            } else {
                matcher.appendReplacement(buffer, matcher.group(0).toLowerCase());
            }
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    public String humpCase(String text) {
        if (StringUtils.isEmpty(text)) {
            return text;
        }
        // 强转全小写
        text = text.toLowerCase();
        Matcher matcher = TO_HUMP_PATTERN.matcher(text.toLowerCase());
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(buffer, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    /**
     * 通过java全名获取类名
     *
     * @param fullName 全名
     * @return 类名
     */
    public String getClsNameByFullName(String fullName) {
        return fullName.substring(fullName.lastIndexOf('.') + 1);
    }

}
